{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Inﬁltration](04.06-Inﬁltration.ipynb) | [Contents](Index.ipynb) | [River Routing–Muskingum method](04.08-River-Routing-Muskingum-method.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.7 地表水\n",
    "\n",
    "矩形渠道中的流量深度由下式给出，\n",
    "\n",
    "<center>$Q=\\frac{1.49}{n}S_0^{1/2}\\frac{(By)^{5/3}}{(B+y)^{2/3}}$,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(4.15)</center>\n",
    "\n",
    "其中，$Q$为流量,$n$为曼宁系数,$S_0$为水面坡度，$B$是渠道宽度，$y$为流量深度。这是$y$中的一个非线性方程，还没有得到显式解。可以像上一节那样迭代解决，或者使用像牛顿迭代法(Newton-Raphson)这样的方法。在这里，我们将使用`Scipy.optimize`库中的`fmin`函数来解决。首先，我们将导入所需的库。然后，我们将定义一个函数，以流量深度(y)作为输入，返回基于这个y和给定的Q估计的流量的误差。我们取误差的绝对值，也可取其他如误差平方等。在指定函数之后，我们可以把这个函数和`y`的一些初始估计值作为`fmin`的输入。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.000078\n",
      "         Iterations: 13\n",
      "         Function evaluations: 26\n"
     ]
    }
   ],
   "source": [
    "# 导入依赖库\n",
    "from __future__ import division\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.optimize import fmin\n",
    "\n",
    "# 定义变量\n",
    "n = 0.015\n",
    "S0 = 0.025\n",
    "Q = 9.26\n",
    "B = 2\n",
    "\n",
    "# 定义流量函数\n",
    "def flow(y):\n",
    "    Q_estiamted = (1.49/n)*(S0**0.5)*((B*y)**(5/3))/((B+y)**(2/3))\n",
    "    epsilon = np.abs(Q_estiamted - Q)\n",
    "    return epsilon\n",
    "\n",
    "y_optimum = fmin(flow,0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`fmin`给我们所需的y值。我们也可以得到迭代的详细信息，以及最终迭代的误差值。我们使用`print`打印y优化值如下。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.52770386]\n"
     ]
    }
   ],
   "source": [
    "print(y_optimum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "优化成功终止，即在默认最大允许迭代次数内实现所需的精度。输出表明需要13次迭代才能达到所需的精度，并且在这个过程中对函数进行了26次求值。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
